package com.gm.commonlib.network;

import android.util.Log;

import com.gm.commonlib.application.MyApplicationHolder;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.network.restful.ResponseException;
import com.gm.commonlib.util.PromptInfoUtils;
import com.gm.commonlib.util.logUtils.LogUtil;
import com.gm.commonlib.views.toast.ToastUtil;
import com.google.gson.JsonSyntaxException;
import com.youth.banner.util.LogUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;
public abstract class BaseHttpObserver<T> extends ObserverImpl<T> {
    public final static String TAG = "BaseHttpObserver";
    public static final int Relogin_Faild_Code = -999;
    protected boolean isRefresh = false;
    protected boolean completedFlag = false;
    IBaseView mIBaseView;
    boolean isAutoErrorToast = true;

    public BaseHttpObserver<T> setAutoErrorToast(boolean autoErrorToast) {
        isAutoErrorToast = autoErrorToast;
        return this;
    }


    public void setBaseView(IBaseView netObserver) {
        this.mIBaseView = netObserver;
    }

    public void setRefresh(boolean isRefresh) {
        this.isRefresh = isRefresh;
    }

    @Override
    public void onError(Throwable e) {
        LogUtil.d("onError: "+e.toString());
        if (e != null) {
            e.printStackTrace();
        }
        try {
            int code = 0;
            if (e instanceof ResponseException) {
                ResponseException responseException = (ResponseException) e;
                BaseResp baseResp = responseException.getBaseResp();
                if (baseResp != null) {
                    handleErrorBaseResp(baseResp);
                }
                return;
            }
            if (e != null) {
                if (e instanceof HttpException) {
                    HttpException exception = (HttpException) e;
                    code = exception.code();
                }
                if (e instanceof ConnectException || e instanceof SocketTimeoutException
                        || e instanceof UnknownHostException) {
                    code = 1009;
                }
                if (e instanceof JsonSyntaxException) {
                    code = 501;
                }
                if (code==401){
                    MyApplicationHolder.getApplication().login(code);
                    return;
                }
            }
                String status_tipinfo = PromptInfoUtils.getDefaultErrorPrompt(code);
                onError(status_tipinfo);

        } catch (Throwable throwable) {

        } finally {
            onComplete();
        }
    }

    @Override
    public void onComplete() {
        super.onComplete();
        completedFlag = true;
        if (mIBaseView != null) {
            mIBaseView.hidLoadingDialog();
        }
    }

    public void cancel() {
        if (!completedFlag) {
            onCancel();
        }
    }

    public void onCancel() {
    }

    protected void onError(String error) {
        if (mIBaseView != null) {
            if (isAutoErrorToast) {
                mIBaseView.showToast(error);
            }
        } else {
            if (isAutoErrorToast) {
                ToastUtil.showToast(error);
            }
        }
    }

    protected void handleErrorBaseResp(BaseResp baseResp) {
        onError(baseResp.getMessage());
    }

    public void onNext(T resp) {
        try {
            if (resp instanceof BaseResp) {
                BaseResp baseResp = (BaseResp) resp;
                if (HttpResultCode.REQUEST_SUCCESS.getCode()==baseResp.getCode()) {
                    handleResultOkResp(resp);
                }
                else {
                    handleResultOtherResp(resp);
                }
            } else {
                handleOriginalResponse(resp);
            }
        } catch (Throwable throwable) {
            LogUtils.d("eee: "+ Log.getStackTraceString(throwable));
            ToastUtil.showToast(throwable.getMessage());
        }
    }

    protected void handleOriginalResponse(T resp) {
    }

    protected void handleResultOkResp(T resp) {

    }

    protected void handleResultNoDataResp(T resp) {

    }

    protected void handleResultOtherResp(T resp) {
        if (resp instanceof BaseResp) {
            onError(((BaseResp) resp).getMessage());
        }
    }

    private Disposable mSubscription = null;

    public void showLoadingDialog() {
        if (mIBaseView != null) {
            mIBaseView.showLoadingDialog();

        }
    }

    public void setSubscription(Disposable result) {
        mSubscription = result;
    }

    public Disposable getSubscription() {
        return mSubscription;
    }
}
